<?php
@session_start();
error_reporting(E_ALL^E_NOTICE^E_WARNING);
require_once 'config.php';//引入配置文件
require_once 'bin.php';//引入数据库链接
class E_8
{

    //流程入口
    public static function __index(){
        //接收群组编号，并简单的识别操作 & 当前关键词位置信息
        $gorupID = (int)$_REQUEST['groupNum'];//接收群组编号
        if( $gorupID <= 99 || $gorupID >= 1000 ){ self::__input( "" );}
        //根据群组编号，定位群信息
        $vbot_Info = M('Vbot')->where( array('groupID' => $gorupID ) )->find();
        if( ! $vbot_Info ){
            self::__input( "提示：当前群尚未开通机器人代开服务,请群主联系天天客服进行处理【天天六胡抢】" );
        }
        //是否存在已经开的但没有满的房间
        self::__olderCreateRoom( $vbot_Info );
        //执行开房操作
        self::__tiantianCreateRoom( $vbot_Info['uid'] , $vbot_Info );
    }

    //天天开房
    public static function __tiantianCreateRoom( $uid , $vbot_Info ){
        $user_info = M('Member')->where( array('groupID' => $uid ) )->find();
        if( !$user_info ){ self::__input( "" ); }
        $consume   = 0;
        //计算当前开房需要消耗多少钻石
        switch ( $vbot_Info['room_js'] ){
            case 1: $consume  = 0;break;
            case 5: $consume  = 2;break;
            case 8: $consume  = 3;break;
            case 16:$consume  = 4;break;
        }
        if( $user_info['level'] >= 1 ){ $consume = 0; }//代理免钻石
        if( $consume >= 1){
            if( $user_info['score'] < $consume ){
                self::__input( "提示：开房失败,群主钻石余额不足或代理已过期【天天六胡抢】" );
            }
            M('Member')->where( array('groupID' => $uid ) )->setDec('score' , $consume );//非代理的情况下,需要进行钻石扣除操作
        }
        //进行添加房间信息操作,蛋疼的PDO
        $data = array();
        $data['room_id']         = self::__getRandRoomID();
        $data['room_cid']        = $uid;
        $data['room_js']         = 1;
        $data['room_zjs']        = $vbot_Info['room_js'];
        $data['room_difen']      = 2;
        $data['room_tp']         = 1;//固定为1，六胡抢
        $data['room_mx']         = $vbot_Info['room_tp'];//六胡抢固定为4个人 , 或者为3人
        $data['room_qh']         = 6;//六胡抢起息为6胡息
        $data['room_mtype']      = $vbot_Info['room_mttype'];//用下划线隔开的玩法
        $data['room_time']       = date('Y-m-d H:i:s',time());
        $data['room_time_micor'] = time();
        $data['consume']         = $consume;
        $data['room_c_level']    = $user_info['level'];//记录开房者当时的代理等级
        $data['is_daikai']       = 1;
        //设置为机器人开房跟地址
        $link                    = self::__getDwz( $data['room_id'] );
        $data['is_vbot']         = 1;
        $data['vbot_link']       = $link;
        $data['vbot_gid']        = $vbot_Info['groupID'];
        //组合sql
        $result                  = M('Room')->data( $data )->add();
        if( !$result ){
            self::__input( "提示：开房失败,请重新发送关键词8进行操作【天天六胡抢】" );
        }
        //判断是三人六胡抢还是四人六胡抢
        if( $vbot_Info['room_tp'] == 4 ){
            $text   = "";
            $text  .= "[Rose][Rose]天天 " . $data['room_id'] . " 速进 【[Wilt]差四】";
            $text  .= "" . $link . " [Rose][Rose]";
            $text  .= "". self::__EscapeMttype( $data['room_mtype'] ) . "【点蓝色链接进入】";
        }else{
            $text   = "";
            $text  .= "[Rose][Rose]天天 " . $data['room_id'] . " 速进 【[Wilt]差三】";
            $text  .= "" . $link . " [Rose][Rose]";
            $text  .= "". self::__EscapeMttype( $data['room_mtype'] );
        }
        self::__input( $text );
    }

    //发送8的时候，是否已经存在当前属于当前群且机器人开的且人数未满的房间，有点话还是发送这个出来
    public static function __olderCreateRoom( $vbot_Info ){
        $map  = array();
        $map['room_cid']    = $vbot_Info['uid'];
        $map['is_vbot']     = 1;
        $map['room_status'] = 0;
        $map['vbot_gid']    = $vbot_Info['groupID'];
        $room_info = M('Room')->where( $map )->order('room_time_micor asc')->find();
        if( ! $room_info ){ return false; }
        //计算几缺几 - 4人 或者 3人处理
        $people = 0;
        foreach ( $room_info as $key => $value ){
            if( strstr( $key , "room_player_" ) != false && $value != 0 ){
                $people++;
            }
        }
        $quejiString = "";
        if( $room_info['room_mx'] == 4 ) {
            switch ( $people ){
                case 0: $quejiString = "【[Wilt]差四】"; break;
                case 1: $quejiString = "【一差三】"; break;
                case 2: $quejiString = "【二差二】"; break;
                case 3: $quejiString = "【三差一】"; break;
            }
            //存在即重新生成分享数据即可
            $text   = "[Rose][Rose]天天 " . $room_info['room_id'] . " 速进 ". $quejiString ."";
            $text  .= "" . $room_info['vbot_link'] . " [Rose][Rose]";
            $text  .= "". self::__EscapeMttype( $room_info['room_mtype'] ) . "【点蓝色链接进入】";
        }else{
            switch ( $people ){
                case 0: $quejiString = "【[Wilt]差三】"; break;
                case 1: $quejiString = "【一差二】"; break;
                case 2: $quejiString = "【二差一】"; break;
            }
            $text   = "[Rose][Rose]天天 " . $room_info['room_id'] . "  速进  ". $quejiString ."\n";
            $text  .= "" . $room_info['vbot_link'] . " [Rose][Rose]";
            $text  .= "". self::__EscapeMttype( $room_info['room_mtype'] ) ;
        }
        //直接输出数据
        self::__input( $text );
    }

    public static function __getRandRoomID(){
        $room_id = "";
        for( $i = 1 ; $i <= 5 ; $i++ ){ $room_id = $room_id .  rand(0,9); }
        $room_id = rand(1,9) . $room_id;
        $room_is = M('Room')->where( array('room_id' => $room_id ) )->find();
        if( $room_is ){
            return self::__getRandRoomID();
        }
        return $room_id;
    }

    public static function __getDwz( $room_id ){
        return 'www.8678u.cn/8.php?r=' . $room_id;
    }

    public static function __EscapeMttype( $mt_type ){
        $typeSplit      = explode("_", $mt_type );
        $stringArray    = array();
        $stringArray[1] = "底分2分";
        $stringArray[2] = "1.5.10";
        $stringArray[3] = "红黑点";
        $stringArray[4] = "天地大小";
        $stringArray[5] = "一点红";
        $stringArray[6] = "碰碰胡";
        $string = "";
        //如果3跟5同时存在的话；5就不要展示了
        if( strstr( $mt_type , "3" ) != false && strstr( $mt_type , "5" ) != false ){
            unset( $stringArray[5] );
        }
        //翻醒跟随醒放前面
        foreach ( $typeSplit as $key => $value ){
            if( $value == 7 ){$string = $string . "翻醒" . ",";}
        }
        foreach ( $typeSplit as $key => $value ){
            if( $value == 8 ){$string = $string . "随醒" . ",";}
        }
        //剩余其他的
        foreach ( $typeSplit as $key => $value ){
            if( $value != 7 && $value != 8 ) {
                if( strstr( $mt_type , "3" ) != false && strstr( $mt_type , "5" ) != false && $value == 5 ){
                    continue;
                }
                $string = $string . $stringArray[ $value ] . " , ";
            }
        }
        $string = $string . "自摸翻倍";
        return $string;
    }

    public static function __input( $text_msg ){
        if( $text_msg == "" ){ echo "";exit; }
        $data = array();$data['status'] = 0;$data['mode']   = 8;$data['text']   = $text_msg;
        $inputText = "";
        foreach ( $data as $key => $value){
            $inputText = $inputText . $value . "|";
        }
        echo $inputText;exit;
    }
}
E_8::__index();